import * as sss from "../api"


export function dateFormat(dateTime, dateFormatStr) {


    var d = dateTime ? new Date(dateTime) : new Date();
    var year = d.getFullYear();
    var month = d.getMonth() + 1;
    var day = d.getDate();
    var hours = d.getHours();
    var min = d.getMinutes();
    var seconds = d.getSeconds();

    if (month < 10) month = '0' + month;
    if (day < 10) day = '0' + day;
    if (hours < 0) hours = '0' + hours;
    if (min < 10) min = '0' + min;
    if (seconds < 10) seconds = '0' + seconds;
    var dateMap = new Map()
    dateMap.set('yyyy', year)
    dateMap.set('MM', month)
    dateMap.set('dd', day)
    dateMap.set('HH', hours)
    dateMap.set('mm', min)
    dateMap.set('ss', seconds)
    var list = dateFormatStr.split(" ")
    var dateStr = ''
    var timeStr = ''
    var dateTimeStr = ''
    if (list.length == 2) {
        var datePart = list[0]
        var timePart = list[1]
        var datePartList = datePart.split("-")
        var timePartList = timePart.split(":")
        datePartList.forEach(item => {
            dateStr = dateStr + dateMap.get(item) + '-'
        })
        timePartList.forEach(item => {
            timeStr = timeStr + dateMap.get(item) + ':'
        })

        dateTimeStr = dateStr.replace(/-+$/, "") + ' ' + timeStr.replace(/:+$/, "")
    }
    if (list.length == 1) {
        var datePart = list[0]

        var datePartList = datePart.split("-")
        datePartList.forEach(item => {
            dateStr = dateStr + dateMap.get(item) + '-'
        })
        dateTimeStr = dateStr.replace(/-+$/, "")
    }
    return dateTimeStr;
}

export function getFieldValue(fieldValue, fieldKey, businessInfo) {
    var fieldCaptionValue = ""
    // console.log("返回字段值")
    // console.log(fieldValue)
    // console.log(businessInfo)
    if (fieldValue != undefined&&businessInfo!==undefined&&businessInfo!==null) {
        var entrys = businessInfo.Result.NeedReturnData.Entrys
        entrys.forEach(entry => {
            entry.Fields.forEach(field => {
                if (field.Key == fieldKey) {
                    // console.log("field")
                    // console.log("field.Key:"+field.Key,"---","fieldKey:"+fieldKey)
                    // console.log(field.ElementType)
                    if (field.ElementType == 9 || field.ElementType == 40) {
                        field.Extends.forEach(extend => {
                            if (extend.Value == fieldValue) {
                                // console.log(fieldValue,"+++++",extend.Caption)
                                fieldCaptionValue = extend.Caption
                            }
                        })
                    }

                }
            })
        })
    }
    return fieldCaptionValue
}
